let Editor = function () {
    let docEditor;

    let onAppReady = function () {
        console.log("OnlyOffice 文档编辑器已准备就绪");
    };

    // 修改文档时调用的函数
    let onDocumentStateChange = function (event) {
        console.log("修改event.data:" + event.data)
        if (event.data) {
            console.log("文件变了");
            // docEditor.downloadAs();
        } else {
            console.log("在文档编辑服务上收集更改");
        }
    };
    window.addEventListener('message', function (e) {
        console.log(e.data)
        if (e.data === "downloadAs") {
            docEditor.downloadAs();
        }
    }, false)

    let onError = function (event) {
        console.log("OnlyOffice 文档编辑器错误: " + event.data.errorCode + ", 详情: " + event.data.errorDescription);
    };
    let onOutdatedVersion = function () {
        location.reload(true);
    };
    let getDocumentConfig = function (document) {
        if (document) {
            return {
                "document": document
            };
        }
        console.log("文档未指定！");
        return null;
    };
    let сonnectEditor = function (document) {

        let config = getDocumentConfig(document);
        config.events = {
            "onAppReady": onAppReady,// 将应用程序加载到浏览器中时调用的函数
            "onDocumentStateChange": onDocumentStateChange,// 修改文档时调用的函数
            "onError": onError,// 发生错误或其他一些特定事件
            "onOutdatedVersion": onOutdatedVersion// 显示错误后调用的函数
        };
        config.editorConfig = document.editorConfig
        $.extend(config.editorConfig);
        console.log(config)
        docEditor = new DocsAPI.DocEditor("iframeEditor", config);
    };

    return {
        init: function (document) {
            $('#left-btn-about').hide();
            сonnectEditor(document);
        }
    }
}();
